home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / AdobeExamples / NX_Scroll / Graphic.m < prev    next >
Encoding:
Text File  |  1992-12-19  |  5.6 KB  |  298 lines

  1.  
  2. /*
  3.  * (a)  (C) 1990 by Adobe Systems Incorporated. All rights reserved.
  4.  *
  5.  * (b)  If this Sample Code is distributed as part of the Display PostScript
  6.  *    System Software Development Kit from Adobe Systems Incorporated,
  7.  *    then this copy is designated as Development Software and its use is
  8.  *    subject to the terms of the License Agreement attached to such Kit.
  9.  *
  10.  * (c)  If this Sample Code is distributed independently, then the following
  11.  *    terms apply:
  12.  *
  13.  * (d)  This file may be freely copied and redistributed as long as:
  14.  *    1) Parts (a), (d), (e) and (f) continue to be included in the file,
  15.  *    2) If the file has been modified in any way, a notice of such
  16.  *      modification is conspicuously indicated.
  17.  *
  18.  * (e)  PostScript, Display PostScript, and Adobe are registered trademarks of
  19.  *    Adobe Systems Incorporated.
  20.  * 
  21.  * (f) THE INFORMATION BELOW IS FURNISHED AS IS, IS SUBJECT TO
  22.  *    CHANGE WITHOUT NOTICE, AND SHOULD NOT BE CONSTRUED
  23.  *    AS A COMMITMENT BY ADOBE SYSTEMS INCORPORATED.
  24.  *    ADOBE SYSTEMS INCORPORATED ASSUMES NO RESPONSIBILITY
  25.  *    OR LIABILITY FOR ANY ERRORS OR INACCURACIES, MAKES NO
  26.  *    WARRANTY OF ANY KIND (EXPRESS, IMPLIED OR STATUTORY)
  27.  *    WITH RESPECT TO THIS INFORMATION, AND EXPRESSLY
  28.  *    DISCLAIMS ANY AND ALL WARRANTIES OF MERCHANTABILITY, 
  29.  *    FITNESS FOR PARTICULAR PURPOSES AND NONINFRINGEMENT
  30.  *    OF THIRD PARTY RIGHTS.
  31.  */
  32.  
  33. /*
  34.  *    Graphic.m
  35.  *
  36.  *    Version:    2.0
  37.  *    Author:    Ken Fromm
  38.  *    History:
  39.  *            03-07-91        Added this comment.
  40.  */
  41.  
  42. #import "Graphic.h"
  43. #import "DrawingViewWraps.h"
  44. #import <appkit/defaults.h>
  45. #import <appkit/nextstd.h>
  46. #import <dpsclient/wraps.h>
  47.  
  48. extern void makeRedBook(UPath *aUpath);
  49.  
  50. void initGparms(GParms *gParms)
  51. {
  52.     gParms->path_type = STROKE;
  53.     gParms->color =  NX_COLORBLACK;
  54.     gParms->linewidth = 1;
  55.     gParms->miterlimit = 10;
  56.     gParms->linejoin = gParms->linecap = 0;
  57. }
  58.  
  59. void setGparms(GParms *gParms)
  60. {
  61.     PSWSetParameters((int)gParms->linejoin, (int) gParms->linecap,
  62.             gParms->linewidth, gParms->miterlimit);
  63. }
  64.  
  65. @implementation Graphic : Object
  66.  
  67. + new
  68. {
  69.     self = [super new];
  70.     initGparms(&parms);
  71.     
  72.     return self;
  73. }
  74.  
  75. - free
  76. {
  77.     if (path.pts)
  78.         NX_FREE(path.pts);
  79.     if (path.ops)
  80.         NX_FREE(path.ops);
  81.  
  82.     return self;
  83. }
  84.  
  85. - installGparms:(const GParms *) gParm
  86. {
  87.     parms = *gParm;
  88.  
  89.     return self;
  90. }
  91.  
  92. - installUpath:(const UPath *) aUpath andBounds:(const NXRect *) aRect
  93. {
  94.     NX_MALLOC(path.pts, float, aUpath->num_pts+4);
  95.     NX_MALLOC(path.ops, char, aUpath->num_ops+1);
  96.  
  97.     bcopy(aUpath->pts, &path.pts[4], aUpath->num_pts * sizeof(float)/sizeof(char));
  98.     path.num_pts = aUpath->num_pts + 4;
  99.  
  100.     path.ops[0] = dps_ucache;
  101.     bcopy(aUpath->ops, &path.ops[1], aUpath->num_ops);
  102.     path.num_ops = aUpath->num_ops + 1;
  103.  
  104.  
  105.     bounds = *aRect;
  106.     path.pts[0] = bounds.origin.x;
  107.     path.pts[1] = bounds.origin.y;
  108.     path.pts[2] = bounds.origin.x + bounds.size.width;
  109.     path.pts[3] = bounds.origin.y + bounds.size.height;
  110.  
  111.     return self;
  112. }
  113.  
  114. - getBounds:(NXRect *)theRect
  115. {
  116.     *theRect = bounds;
  117.  
  118.     return self;
  119. }
  120.  
  121. /* Private methods sometimes overridden by subclassers */
  122. - setPSState:(GParms *) gParms
  123. {
  124.     if (!gParms)
  125.         setGparms(&parms);
  126.     else
  127.     {
  128.         if (parms.linewidth != gParms->linewidth)
  129.             PSsetlinewidth(parms.linewidth);
  130.         if (parms.linejoin != gParms->linejoin)
  131.             PSsetlinejoin((int) parms.linejoin);
  132.         if (parms.linecap != gParms->linecap)
  133.             PSsetlinecap((int) parms.linecap);
  134.         if (parms.miterlimit != gParms->miterlimit)
  135.             PSsetmiterlimit(parms.miterlimit);
  136.     }
  137.  
  138.     if (!gParms || !NXEqualColor(gParms->color, parms.color))
  139.         NXSetColor(parms.color);
  140.  
  141.     if (gParms)
  142.         *gParms = parms;
  143.  
  144.     return self;
  145. }
  146.  
  147. /* Public routines. */
  148. - setPathType:(int) value
  149. {
  150.     parms.path_type = (unsigned char) value;
  151.  
  152.     return self;
  153. }
  154.  
  155. - (int)pathType
  156. {
  157.     return  (int)parms.path_type;
  158. }
  159.  
  160. - setLineWidth:(float) value
  161. {
  162.     parms.linewidth = value;
  163.  
  164.     return self;
  165. }
  166.  
  167. - (float)lineWidth
  168. {
  169.     return parms.linewidth;
  170. }
  171.  
  172. - setLineJoin:(int) value
  173. {
  174.     parms.linejoin = (unsigned char) value;
  175.  
  176.     return self;
  177. }
  178.  
  179. - (int)lineJoin
  180. {
  181.     return (int) parms.linejoin;
  182. }
  183.  
  184. - setLineCap:(int) value
  185. {
  186.     parms.linecap = (unsigned char) value;
  187.  
  188.     return self;
  189. }
  190.  
  191. - (int)lineCap
  192. {
  193.     return (int) parms.linecap;
  194. }
  195.  
  196. - setMiterLimit:(float) value
  197. {
  198.     parms.miterlimit = value;
  199.  
  200.     return self;
  201. }
  202.  
  203. - (float) miterLimit
  204. {
  205.     return parms.miterlimit;
  206. }
  207.  
  208. - (float)gray
  209. {
  210.     float        value;
  211.  
  212.     NXConvertColorToGray(parms.color, &value);
  213.  
  214.     return value;
  215. }
  216.  
  217. - setGray:(float) aGray
  218. {
  219.     parms.color = NXConvertGrayToColor(aGray);
  220.  
  221.     return self;
  222. }
  223.  
  224. - (NXColor) color;
  225. {
  226.     return parms.color;
  227. }
  228.  
  229. - setColor:(NXColor)aColor
  230. {
  231.     parms.color = aColor;
  232.  
  233.     return self;
  234. }
  235.  
  236. /*
  237. *    Returns the user path description for the user path. If a
  238. *    rectangle is passed in then the user path is returned only
  239. *    if it lies within the rectangle.
  240. */
  241. - getUpath:(UPath **) aUpath  forRect:(NXRect *) r
  242. {
  243.     if (!r || NXIntersectsRect(r, &bounds))
  244.         *aUpath = &path;
  245.     else
  246.         *aUpath = NULL;
  247.     
  248.     return self;
  249. }
  250.  
  251. /*
  252.  * Draws the graphic.
  253.  */
  254. - drawObject:(NXRect *) r  currentParms:(GParms *) gParms
  255.     withManner:(int) manner  timingInfo:(Timing *) timing
  256. {
  257.     int        path_type;
  258.     
  259.     if (!r || NXIntersectsRect(r, &bounds))
  260.     {
  261.         timing->num_subpaths++;
  262.         if (parms.path_type == FILL)
  263.         {
  264.             timing->num_fills++;
  265.             path_type = dps_ufill;
  266.         }
  267.         else
  268.         {
  269.             timing->num_strokes++;
  270.             path_type = dps_ustroke;
  271.         }        
  272.         
  273.         [self setPSState:gParms];
  274.         if (manner == REDBOOK)
  275.         {
  276.             makeRedBook(&path);
  277.             if (parms.path_type == FILL)
  278.                 PSfill();
  279.             else
  280.                 PSstroke();
  281.         }
  282.         else
  283.         {
  284.             if (manner == UCACHE)
  285.                 DPSDoUserPath(&path.pts[4], path.num_pts-4, dps_float,
  286.                     &path.ops[0], path.num_ops, path.pts, path_type);
  287.             else
  288.                 DPSDoUserPath(&path.pts[4], path.num_pts-4, dps_float,
  289.                 &path.ops[1], path.num_ops-1, path.pts, path_type);
  290.         }
  291.     }
  292.  
  293.     return self;
  294. }
  295.  
  296. @end
  297.  
  298.